package cz.mpelant.msoe.mealchecker.parser;

import java.util.regex.MatchResult;
import java.util.regex.Pattern;

import cz.mpelant.msoe.mealchecker.data.HistoryRow;

public class HistoryParser extends Parser {


	public static String getTable(String data) {
		return getMatch(data, Pattern.compile("<table[^\n]*<thead>(.*)</table>", Pattern.MULTILINE | Pattern.DOTALL)).group(1);
	}

	public static String[] getRows(String table) {
		return table.split("<tr class=\"row");
	}

	public static HistoryRow getRow(String row) {
		if (row.contains("Default Discount Plan"))
			return null;

		MatchResult m = getMatch(
				row,
				Pattern.compile(">([0-9]+/[0-9]+/[0-9]+[^<]+)" + tdEnd + td + value + tdEnd + td + a + value + aEnd + tdEnd + td + value + tdEnd + td + value + tdEnd, Pattern.MULTILINE
						| Pattern.DOTALL));
		HistoryRow r = new HistoryRow();
		r.date = m.group(1).trim();
		r.location = m.group(2).trim();
		r.account = m.group(3).trim();
		r.type = m.group(4).trim();
		r.amount = m.group(5).trim();
		r.amount = invertSigns(r.amount);
		return r;

	}

	public static String invertSigns(String value) {
		if (value.contains("-"))
			value = value.replace("-", "");
		else
			value = "-" + value;
		return value;
	}
}
